package com.vnt.lucene;

import com.vnt.domain.Book;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.util.Version;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MySearcherManager {
    LuceneUtil luceneUtil = null;
    String indexDir = null;
    public MySearcherManager(String indexDir){
        this.indexDir = indexDir;
        this.luceneUtil = new LuceneUtil(indexDir);
    }
    public static void testSearch(){
        MySearcherManager mySearcherManager = new MySearcherManager("D:/VNThanh/Dev/SpringFramework/Source/workspaceJeeKepler/SpringMVC/mydemoindex");
        try {
            mySearcherManager.luceneUtil.createIndexSearcher();
            IndexSearcher indexSearcher =   mySearcherManager.luceneUtil.getIndexSearcher();
            
            List<Book> bookList =   mySearcherManager.getBooksByField("Book_author 2", "author", indexSearcher);
            System.out.println("numResults of getBooksByField('Book_author 2', 'author') : " + bookList.size());
            for (Book book : bookList) {
                System.out.println(book.getBook_name());
                System.out.println(book.getBook_author());
                System.out.println(book.getCategory());
                System.out.println(book.getNumpages());
                System.out.println(book.getPrice());
            }

            bookList =   mySearcherManager.getWildBooksByField("Book_author 1*", "author", indexSearcher);
            System.out.println("numResults of getWildBooksByField('Book_author*', 'author') : " + bookList.size());
            for (Book book : bookList) {
                System.out.println(book.getBook_name());
                System.out.println(book.getBook_author());
                System.out.println(book.getCategory());
                System.out.println(book.getNumpages());
                System.out.println(book.getPrice());
            }

            /*
            */
            bookList =   mySearcherManager.getPhraseBooksByField("Book_author", "name", indexSearcher);
            System.out.println("numResults of getPhraseBooksByField('Book_author', 'name') : " + bookList.size());
            for (Book book : bookList) {
                System.out.println(book.getBook_name());
                System.out.println(book.getBook_author());
                System.out.println(book.getCategory());
                System.out.println(book.getNumpages());
                System.out.println(book.getPrice());
            }
			

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Book> getBooksByField(String value, String field, IndexSearcher indexSearcher){
        List<Book> bookList = new ArrayList<Book>();
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
        QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);

        try {
            BooleanQuery query = new BooleanQuery();
            query.add(new TermQuery(new Term(field, value)), BooleanClause.Occur.MUST);
            int numResults = 100;
            ScoreDoc[] hits =   indexSearcher.search(query,numResults).scoreDocs;
            for (int i = 0; i < hits.length; i++) {
                Document doc = indexSearcher.doc(hits[i].doc);
                bookList.add(getBook(doc));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bookList;
    }

    public List<Book> getWildBooksByField(String value, String field, IndexSearcher indexSearcher){
        List<Book> bookList = new ArrayList<Book>();
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
        QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);

        try {
            BooleanQuery query = new BooleanQuery();
            query.add(new WildcardQuery(new Term(field, value)), BooleanClause.Occur.SHOULD);
            int numResults = 100;
            ScoreDoc[] hits =   indexSearcher.search(query,numResults).scoreDocs;
            for (int i = 0; i < hits.length; i++) {
                Document doc = indexSearcher.doc(hits[i].doc);
                bookList.add(getBook(doc));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bookList;
    }


     public List<Book> getPhraseBooksByField(String value, String field, IndexSearcher indexSearcher){
        List<Book> bookList = new ArrayList<Book>();
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
        QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);

        try {
            PhraseQuery query = new PhraseQuery();
            //query.setSlop(2);
            query.add(new Term(field,value));
            //query.add(new Term(field,"2"));
            int numResults = 100;
            ScoreDoc[] hits =   indexSearcher.search(query,numResults).scoreDocs;
            for (int i = 0; i < hits.length; i++) {
                Document doc = indexSearcher.doc(hits[i].doc);
                bookList.add(getBook(doc));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bookList;
    }

    public Book getBook(Document doc){
        Book book = new Book();
        book.setBook_name(doc.get("name"));
        book.setBook_author(doc.get("author"));
        book.setCategory(doc.get("category"));
        book.setNumpages(Integer.parseInt(doc.get("numpage")));
        book.setPrice(Float.parseFloat(doc.get("price")));

        return book;
    }
}
